package edu.szcu.vitaguard.controller;

import edu.szcu.vitaguard.entity.Prescription;
import edu.szcu.vitaguard.service.PrescriptionService;
import edu.szcu.vitaguard.vo.PageResult;
import edu.szcu.vitaguard.vo.PrescriptionQuery;
import edu.szcu.vitaguard.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 处方控制器
 */
@RestController
@RequestMapping("/prescription")
public class PrescriptionController {

    @Autowired
    private PrescriptionService prescriptionService;

    /**
     * 分页查询处方列表
     *
     * @param query 查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<PageResult<Prescription>> findByPage(PrescriptionQuery query) {
        PageResult<Prescription> pageResult = prescriptionService.listPrescriptions(query);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询处方详情
     *
     * @param id 处方ID
     * @return 处方详情
     */
    @GetMapping("/{id}")
    public Result<Prescription> findById(@PathVariable Integer id) {
        Prescription prescription = prescriptionService.getPrescriptionById(id);
        if (prescription != null) {
            return Result.success(prescription);
        } else {
            return Result.error("处方不存在");
        }
    }

    /**
     * 添加处方
     *
     * @param prescription 处方信息
     * @return 添加结果（成功返回处方ID）
     */
    @PostMapping
    public Result<Integer> add(@RequestBody Prescription prescription) {
        Integer prescriptionId = prescriptionService.addPrescription(prescription);
        if (prescriptionId != null) {
            return Result.success("添加成功", prescriptionId);
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 更新处方
     *
     * @param prescription 处方信息
     * @return 更新结果
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody Prescription prescription) {
        boolean result = prescriptionService.updatePrescription(prescription);
        if (result) {
            return Result.success("更新成功", true);
        } else {
            return Result.error("更新失败");
        }
    }

    /**
     * 删除处方
     *
     * @param id 处方ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Integer id) {
        boolean result = prescriptionService.deletePrescription(id);
        if (result) {
            return Result.success("删除成功", true);
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 发药
     *
     * @param id 处方ID
     * @return 发药结果
     */
    @PutMapping("/dispense/{id}")
    public Result<Boolean> dispense(@PathVariable Integer id) {
        boolean result = prescriptionService.dispense(id);
        if (result) {
            return Result.success("发药成功", true);
        } else {
            return Result.error("发药失败");
        }
    }

    /**
     * 退药
     *
     * @param id 处方ID
     * @return 退药结果
     */
    @PutMapping("/return/{id}")
    public Result<Boolean> returnMedicine(@PathVariable Integer id) {
        boolean result = prescriptionService.returnMedicine(id);
        if (result) {
            return Result.success("退药成功", true);
        } else {
            return Result.error("退药失败");
        }
    }
}
